

#region References

using System;
using System.Collections.Generic;
using System.Linq;
using System.IO;

using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;

using Freeform;

#endregion



namespace Space_in_Shaders
{
    /// <summary>
    /// This is the main type for your game
    /// </summary>
    public class Game : Freeform.Game
    {

        #region Program Entry Point

        static void Main(string[] args) { using (Game game = new Game()) { game.Run(); } }

        #endregion


        #region Members

        public String TextureTheme { get; set; }

        #endregion


        #region Init

        public Game() 
            : base(new Vector2(1600, 900), true)
        {
        }

        /// <summary>
        /// Allows the game to perform any initialization it needs to before starting to run.
        /// This is where it can query for any required services and load any non-graphic
        /// related content.  Calling base.Initialize will enumerate through any components
        /// and initialize them as well.
        /// </summary>
        protected override void Initialize()
        {
            base.Initialize();

            // Set game options.
            IsMouseVisible = false;
            TextureTheme = "LowQuality";

            // Create default game binds.
            KeyBinder.AddBind("Up", Keys.W);
            KeyBinder.AddBind("Down", Keys.S);
            KeyBinder.AddBind("Left", Keys.A);
            KeyBinder.AddBind("Right", Keys.D);

            // Create game world.
            new SpaceWorld(this);
        }

        #endregion


        #region Update

        /// <summary>
        /// Allows the game to run logic such as updating the world,
        /// checking for collisions, gathering input, and playing audio.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        protected override void Update(GameTime gameTime)
        {
            base.Update(gameTime);

            if (Input.KeyState_Current.IsKeyDown(Keys.Escape)) Exit();
        }

        #endregion


        #region Utility

        public AnimationSet LoadAnimation(String Name)
        {
            String Direc = "Textures/" + TextureTheme + "/";
            DirectoryInfo Dir = new DirectoryInfo(Content.RootDirectory + "/" + Direc);

            if (Dir == null) return null;

            FileInfo[] Files = Dir.GetFiles(Name + "*");
            AnimationSet Animations = new AnimationSet();

            foreach (FileInfo F in Files)
            {
                String[] C = F.Name.Split(new char[] {'_', '.'}, 6);
                Animations.AddAnimation(new Animation(Direc + F.Name.Split(new char[] { '.' }, 2)[0], C[1], Int32.Parse(C[2]), Int32.Parse(C[3]), Int32.Parse(C[4])));
            }

            return Animations;
        }


        public void LoadAnimation(Entity Ent)
        {
            String ClassName = Ent.GetType().Name;
            String Direc = "Textures/" + TextureTheme + "/";
            DirectoryInfo Dir = new DirectoryInfo(Content.RootDirectory + "/" + Direc);

            if (Dir == null) return;

            FileInfo[] Files = Dir.GetFiles(ClassName + "*");
            AnimationSet Animations = new AnimationSet();

            foreach (FileInfo F in Files)
            {
                String[] C = F.Name.Split(new char[] { '_', '.' }, 6);
                Animations.AddAnimation(new Animation(Direc + F.Name.Split(new char[] { '.' }, 2)[0], C[1], Int32.Parse(C[2]), Int32.Parse(C[3]), Int32.Parse(C[4])));
            }

            new Actor_Image2D(Ent, Animations);
        }


        public void LoadAnimation(Entity Ent, string CustomClassName)
        {
            String Direc = "Textures/" + TextureTheme + "/";
            DirectoryInfo Dir = new DirectoryInfo(Content.RootDirectory + "/" + Direc);

            if (Dir == null) return;

            FileInfo[] Files = Dir.GetFiles(CustomClassName + "*");
            AnimationSet Animations = new AnimationSet();

            foreach (FileInfo F in Files)
            {
                String[] C = F.Name.Split(new char[] { '_', '.' }, 6);
                Animations.AddAnimation(new Animation(Direc + F.Name.Split(new char[] { '.' }, 2)[0], C[1], Int32.Parse(C[2]), Int32.Parse(C[3]), Int32.Parse(C[4])));
            }

            new Actor_Image2D(Ent, Animations);
        }

        #endregion

    }
}
